﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using NUnit.Framework;
using Selenium;

namespace Medianamik.Tests.Selenium
{
    public class SeleniumManager
    {
        public ISelenium selenium { get; set; }
        public StringBuilder verificationErrors { get; set; }
        public bool IsLogged { get; set; }
        public bool IsRunning { get; set; }
        public bool LastActionWasARestore { get; set; }

        public static string BaseTestUrl
        {
            get
            {   string value = ConfigurationManager.AppSettings["BaseTestUrl"];
                if(value.EndsWith("/"))
                    return value;
                return value + "/";
            }
        }

        public SeleniumManager()
            :this(ConfigurationManager.AppSettings["TestServer"])
        {
        }

        public SeleniumManager(string srv)
            : this(srv, Int32.Parse(ConfigurationManager.AppSettings["TestServerPort"]))
        {

        }
        public SeleniumManager(string srv, int port)
            : this(srv, port, ConfigurationManager.AppSettings["TestBrowser"])
        {
        }
        public SeleniumManager(string srv, int port, string browser)
        {
            selenium = new DefaultSelenium(srv, port, browser, BaseTestUrl);

            verificationErrors = new StringBuilder();
            IsLogged = false;
            IsRunning = false;
            LastActionWasARestore = false;
        }

        public void Start()
        {
            LastActionWasARestore = false;
            selenium.Start();
            IsRunning = true;
        }

        public void Stop()
        {
            LastActionWasARestore = false;
            selenium.Stop();
            IsRunning = false;
        }

        public void RestoreDatabaseState()
        {
            if (!LastActionWasARestore)
            {
                RestoreDatabase();
                LastActionWasARestore = true;
            }
        }

        public void LogToMedianamik()
        {
            LastActionWasARestore = false;
            if (IsRunning)
            {
                selenium.Open("/Medianamik/Login.aspx");
                selenium.Type("ctl00_MainContentHolder_MedianamikLogin_UserName", "sa");
                selenium.Type("ctl00_MainContentHolder_MedianamikLogin_Password", "sa");
                selenium.Click("ctl00_MainContentHolder_MedianamikLogin_cmdLogin");
                selenium.WaitForPageToLoad("30000");
                Assert.AreEqual(BaseTestUrl + "medianamik/masterpanel.aspx",
                                selenium.GetLocation().ToLower());
                IsLogged = true;
            }
            else
            {
                Assert.Fail("TEST ERROR: Selenium session is not started");
            }
        }

        public void LogOut()
        {
            LastActionWasARestore = false;
            selenium.Click("ctl00_ctl00_cmdLogOut");
            selenium.WaitForPageToLoad("30000");
        }

        private static void RestoreDatabase()
        {
            //Restore Database
            var cn =
                new SqlConnection(ConfigurationManager.ConnectionStrings["RestoreCS"].ToString());
            cn.Open();

            string dbName = ConfigurationManager.AppSettings["TestDatabase"].ToString();
            var cmd = new SqlCommand(String.Format("exec master.dbo.RestoreTestDatabase '{0}'",dbName), cn);
            cmd.ExecuteNonQuery();
            cn.Close();

           
        }
    }
}